//
//  MineProfileDetailViewController.swift
//  MT
//
//  Created by M on 2020/1/5.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
/// 网络、通用
import MTCategory

import Moya
import Result
import RxSwift
import RxCocoa
/// 其它
import YPNavigationBarTransition
import JXPagingView
import JXCategoryView

class MineProfileDetailViewController: BaseViewController {
    
    var model: MineInfoModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    /// 滑动回调
    var tableViewDidScrollCallBack: ((UIScrollView) -> Swift.Void)?
    
    lazy var tableView: UITableView = {
        let tv = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .grouped)
        tv.backgroundColor = UIColor.theme.background
        tv.delegate = self
        tv.dataSource = self
        tv.contentInsetAdjustmentBehavior = .never
        tv.register(MinePersonalDetailBodyCell.self, forCellReuseIdentifier: String(describing: MinePersonalDetailBodyCell.self))
        tv.register(MinePersonalDetailOccupationSchoolCell.self, forCellReuseIdentifier: String(describing: MinePersonalDetailOccupationSchoolCell.self))
        tv.register(MineTagsTableViewCell.self, forCellReuseIdentifier: String(describing: MineTagsTableViewCell.self))
        return tv
    }()
    
}

// MARK: ------------------------------------ Overrides ----------------------------------------
extension MineProfileDetailViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initializeUI()
    }
    
}


// MARK: ---------------------------------------- 初始化 --------------------------------------------
extension MineProfileDetailViewController {
    
    private func initializeUI() {
        view.backgroundColor = .black
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
}


// MARK: ---------------------------------------- 事件方法 --------------------------------------------
@objc
extension MineProfileDetailViewController {
    
}

// MARK: ------------------ 所有代理 ----------------------
// MARK: ------------------ JXPagingViewListViewDelegate ----------------------
extension MineProfileDetailViewController: JXPagingViewListViewDelegate {
    
    func listView() -> UIView {
        self.view
    }
    
    func listScrollView() -> UIScrollView {
        self.tableView
    }
    
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        self.tableViewDidScrollCallBack = callback
    }
    
}

// MARK: ------------------ UITableViewDelegate ----------------------
extension MineProfileDetailViewController: UITableViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if let callback = tableViewDidScrollCallBack {
            callback(scrollView)
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
    }
    
}

// MARK: ------------------ UITableViewDataSource ----------------------
extension MineProfileDetailViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rows = 2
        if model?.tagList?.count != 0 {
            rows += 1
        }
        if model?.hobbyList?.count != 0 {
            rows += 1
        }
        return rows
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 110
        }
        if indexPath.row == 1 {
            return 90
        }
        if model?.tagList?.count != 0, indexPath.row == 2, let models = model?.tagList {
            return MineTagsTableViewCell.height(for: models) + 50.0
        }
        if model?.hobbyList?.count != 0, (indexPath.row == 2 || indexPath.row == 3), let models = model?.hobbyList {
            return MineTagsTableViewCell.height(for: models) + 50.0
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if model?.tagList?.count != 0, indexPath.row == 2, let models = model?.tagList {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MineTagsTableViewCell.self), for: indexPath) as? MineTagsTableViewCell else {
                return tableView.dequeueReusableCell(withIdentifier: String(describing: MineTagsTableViewCell.self), for: indexPath)
            }
            cell.type = .tags
            cell.screenMode = 1
            cell.colorStyle = .backgroundBlack
            cell.models = models
            return cell
        }
        if model?.hobbyList?.count != 0, (indexPath.row == 2 || indexPath.row == 3), let models = model?.hobbyList {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MineTagsTableViewCell.self), for: indexPath) as? MineTagsTableViewCell else {
                return tableView.dequeueReusableCell(withIdentifier: String(describing: MineTagsTableViewCell.self), for: indexPath)
            }
            cell.type = .hobbies
            cell.screenMode = 1
            cell.colorStyle = .backgroundBlack
            cell.models = models
            return cell
        }
        
        if indexPath.row == 0 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MinePersonalDetailBodyCell.self), for: indexPath) as? MinePersonalDetailBodyCell else {
                return tableView.dequeueReusableCell(withIdentifier: String(describing: MinePersonalDetailBodyCell.self), for: indexPath)
            }
            cell.model = self.model
            return cell
        }
        
        if indexPath.row == 1 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MinePersonalDetailOccupationSchoolCell.self), for: indexPath) as? MinePersonalDetailOccupationSchoolCell else {
                return tableView.dequeueReusableCell(withIdentifier: String(describing: MinePersonalDetailOccupationSchoolCell.self), for: indexPath)
            }
            cell.model = self.model
            return cell
        }
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }
    
}
